<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
$merchid=$this->checklogin();
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'订单列表'),
	/*
	array('op'=>'list0','title'=>'未支付订单列表'),
	array('op'=>'list1','title'=>'已支付订单列表'),
	array('op'=>'list2','title'=>'已确认订单列表'),
	array('op'=>'list3','title'=>'已取消订单列表'),
	array('op'=>'list4','title'=>'已完成订单列表'),
	array('op'=>'list5','title'=>'申请退款订单列表'),
	array('op'=>'list6','title'=>'已退款订单列表'),
	array('op'=>'list7','title'=>'拒绝退款订单列表'),
	*/
	
);
$return['status']=array('0'=>'未支付','1'=>'已支付','2'=>'已确认','3'=>'已取消','4'=>'已完成','5'=>'申请退款','6'=>'已退款','7'=>'拒绝退款');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid']=$merchid;
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','serve_id','package','user_id','worker_id','serve_time','day','timeid','money','status','tuikuan_time','pay','coupon','createtime');
	$list=pdo_getslice('jy_gohome_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_gohome_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['worker_name']=pdo_getcolumn('jy_gohome_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['serve_title']=pdo_getcolumn('jy_gohome_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$user=pdo_get('jy_gohome_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['username']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$item['day'].'--'.$serve_times[$item['timeid']]['times']['start'].'-'.$serve_times[$item['timeid']]['times']['end'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'username','title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'user_mobile','title'=>'用户电话','type'=>'string'),
			array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
			array('name'=>'package','title'=>'套餐','type'=>'string'),
			array('name'=>'serve_time','title'=>'服务时间','type'=>'string'),
			array('name'=>'pay','title'=>'金额','type'=>'string'),
			array('name'=>'coupon','title'=>'优惠券','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'自定义表单数据'),
			array('type'=>'post','op'=>'confirm','title'=>'确认','message'=>'确认接收订单？','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'cancel','title'=>'取消','message'=>'取消订单？','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'agree','title'=>'同意退款','message'=>'确定退款吗？','show'=>array('status'=>5)),
			array('type'=>'post','op'=>'disagree','title'=>'拒绝退款','message'=>'拒绝退款吗？','show'=>array('status'=>5)),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','width'=>15,'title'=>'员工姓名','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'select','options'=>'status_select'),
		)
	);
	$return['status_select']=array(array('id'=>'0','title'=>'待支付'),array('id'=>'1','title'=>'已支付'),
	array('id'=>'2','title'=>'已确认'),array('id'=>'3','title'=>'已取消'),array('id'=>'4','title'=>'已完成'),
	array('id'=>'5','title'=>'申请退款'),array('id'=>'6','title'=>'已退款'),array('id'=>'7','title'=>'拒绝退款'));
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
//确认订单操作
if($_RQ['op']=='confirm'){
	if($_RQ['id']){
	
		pdo_update('jy_gohome_order',array('status'=>'2'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('确认订单成功！');
}
//取消订单操作
if($_RQ['op']=='cancel'){
	if($_RQ['id']){
	
		pdo_update('jy_gohome_order',array('status'=>'3'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('取消订单成功！');
}
//同意退款
if($_RQ['op']=='agree'){
	if($_RQ['id']){
	
		pdo_update('jy_gohome_order',array('status'=>'6'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('已同意退款申请！');
}
//拒绝退款操作
if($_RQ['op']=='disagree'){
	if($_RQ['id']){
	
		pdo_update('jy_gohome_order',array('status'=>'7'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('已拒绝退款申请！');
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_gohome_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择订单','error');
	}
	
	$item['data']=pdo_getall('jy_gohome_data',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$id));
	$formtypes=get_form_types();
	
	foreach($item['data'] as &$v){
		$v['title']=pdo_getcolumn('jy_gohome_fileds',array('id'=>$v['fileds_id'],'uniacid'=>$_SESSION['uniacid']),'title');
		$v['type']=pdo_getcolumn('jy_gohome_fileds',array('id'=>$v['fileds_id'],'uniacid'=>$_SESSION['uniacid']),'type');
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	//print_r($item);exit;
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回订单列表'),
		),
		'desc'=>'表单信息',
		'look'=>array(
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'data',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}